const constant = require('./constant');
const api = require('./api');
const storage = require('./storage');
class HttpRequest{
  constructor(url){
    this.url = url;
    this.header = {'Deviceid':'loveqing'};
    // token失效的请求队列
    this.qeueu = [];
    this.isToGetNewToken = true;
  }
  checkedSuccess(options,token,resolve,reject){
    this.header['Authorization'] = 'Bearer '+ token;
    wx.request({
      url: this.url+options.url,
      method:options.method,
      data:options.data,
      header: this.header,
      success: res => {  // code:50005 token过期
        // return res.data
        resolve(res.data);
      },
      fail: err => {
        reject()
      },
      complete:()=>{
        wx.hideLoading()
      }
    })
  }
  request(options){
      return new Promise((resolve,reject) => {
        this.header['Authorization'] = 'Bearer '+ wx.getStorageSync(storage.LOCAL_JWT_KEY);
        wx.request({
          url: this.url+options.url,
          method:options.method,
          data:options.data,
          header: this.header,
          success: res => {  // code:50005 token过期
            // return res.data
            if(res.data.code == 0){
              resolve(res.data);
            }else{
              console.log('token失效的请求',options);
              if(wx.getStorageSync(storage.LOGINSTATUS) === 1){ // 微信方式登录
                this.qeueu.push(options);
                console.log('查看isToGetNewToken',this.isToGetNewToken);
                if(this.isToGetNewToken){
                  this.isToGetNewToken = false;
                  api.login().then(res => {
                    console.log('查看是否获取了新的token',res);
                    if(res.code == 0){ // 获取新token成功
                      this.isToGetNewToken = true;
                      // 重新执行请求队列
                      this.qeueu.forEach((item,index) => {
                        console.log('查看队列是谁',item);
                        this.checkedSuccess(item,res.data.data.token,resolve,reject);
                        if(index === this.qeueu.length-1){
                          this.qeueu = [];
                        }
                      })
                    }else{ // 获取新token失败，清空请求队列不执行
                      this.qeueu = [];
                    }
                  })
                }
              }else{ // 不是微信登录方式，跳转到登录界面重新登录
                console.log("跳转")
                var pages = getCurrentPages(); //获取加载的页面
                var currentPage = pages[pages.length-1] //获取当前页面的对象
                var url = currentPage.route //当前页面url
                wx.reLaunch({
                  url: '/pages/login/login?route=' + url
                })
              }
              
              
            }
          },
          fail: err => {
            reject()
          },
          complete:()=>{
            wx.hideLoading()
          }
        })
          // api.checkToken().then(res => {
          //   // token验证通过
          //   if(res.code == 0){
          //     console.log('token有效');
          //     this.checkedSuccess(options,res.token,resolve,reject);
          //   }else{
          //     console.log('token失效');
          //     if(wx.getStorageInfoSync(storage.setLoginStatus == 1)){
          //       api.login().then(res => {
          //         if(res.code == 0){
          //           console.log('获取到新token，往下执行')
          //           this.checkedSuccess(options,res.token,resolve,reject);
          //         }
          //       });
          //     }else{
          //       wx.reLaunch({
          //         url: '/pages/login/login',
          //       })
          //     }
          //   }
          // });
      })
  }
}
module.exports = new HttpRequest(constant.BASEURL)
